package com.zhuxulin.android.net;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

public class Request {
	/** URL编码类型，比如：utf-8，GBK，默认utf-8 */
	private String encodeType = "utf-8";
	private Method method = null;
	private String path = null;
	private String host = null;
	private int port = 80;
	private HashMap<String, String> params = null;

	public Request() {
		this(null, null, 80, null);
	}

	public Request(Method method, String path) {
		this(method, null, 80, path);
	}

	public Request(Method method, String host, String path) {
		this(method, host, 80, path);
	}

	public Request(Method method, String host, int port, String path) {
		this.method = method;
		this.port = port;
		this.host = host;
		this.path = path;
		params = new HashMap<String, String>();
	}

	public Method getMethod() {
		return method;
	}

	public void setMethod(Method method) {
		this.method = method;
	}

	public String getPath() {
		return path;
	}

	/** 以 / 开始 */
	public void setPath(String path) {
		this.path = path;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getEncodeType() {
		return encodeType;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	/** URL编码方式，utf-8等，默认utf-8 */
	public void setEncodeType(String encodeType) {
		this.encodeType = encodeType;
	}

	/** 经过了UrlEncodedFormEntity编码的，默认utf-8 */
	public HttpEntity getEntity() {
		List<BasicNameValuePair> pair = new ArrayList<BasicNameValuePair>();
		for (String key : params.keySet()) {
			pair.add(new BasicNameValuePair(key, params.get(key)));
		}
		try {
			return new UrlEncodedFormEntity(pair, encodeType);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

	/** 返回查询路径 比如：?name=dsf&id=sdlsfj... */
	public String getQueryString() {
		StringBuffer qs = new StringBuffer("?");
		for (String key : params.keySet()) {
			qs.append(key + "=" + params.get(key) + "&");
		}
		qs.deleteCharAt(qs.length() - 1);
		return qs.toString();
	}

	/** 返回完整的请求网址 ，不包含请求参数 比如：www.baidu.com/mus */
	public String getURI() {
		if (this.port == 80) {
			return getHost() + getPath();
		} else {
			return getHost() + ":" + getPort() + getPath();
		}
	}

	/** 返回完整的网址，包含参数比如：www.baidu.com/mus?name=dsf&id=sdlsfj... */
	public String getURL() {
		if (this.port == 80) {
			return getHost() + getPath() + getQueryString();
		} else {
			return getHost() + ":" + getPort() + getPath() + getQueryString();
		}
	}

	public void putParam(String key, String value) {
		params.put(key, value);
	}

	public void removeParam(String key) {
		params.remove(key);
	}

	@Override
	public String toString() {
		return String.format("%s %s", method.toString(), path);
	}

	public static enum Method {
		GET {
			@Override
			public String toString() {
				return "GET";
			}
		},
		POST {
			@Override
			public String toString() {
				return "POST";
			}
		},
		PUT {
			@Override
			public String toString() {
				return "PUT";
			}
		},
		DELETE {
			@Override
			public String toString() {
				return "DELETE";
			}
		};

		public abstract String toString();
	}
}
